package com.uplooking.util;

import java.io.UnsupportedEncodingException;
import java.util.Date;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTCreationException;
import com.auth0.jwt.exceptions.JWTVerificationException;

public class TokenUtils {
	//产生令牌
	public static String createToken(String audience,String secret,long expire) throws IllegalArgumentException, JWTCreationException, UnsupportedEncodingException {
		long now = System.currentTimeMillis();
		return JWT.create()
				.withAudience(audience)
				.withIssuedAt(new Date(now))
				.withExpiresAt(new Date(now+expire))
				.sign(Algorithm.HMAC256(secret));
	}
	
	//获取数据
	public static String getAudience(String token) {
		return JWT.decode(token).getAudience().get(0);
	}
	
	//验证令牌
	public static void checkToken(String token,String secret) throws JWTVerificationException, IllegalArgumentException, UnsupportedEncodingException {
		JWT.require(Algorithm.HMAC256(secret)).build().verify(token);
	}
}
